package com.ldc.wandroid.ui.fragments;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.ldc.common.model.BaseModel;
import com.ldc.common.mvvm.BaseMvvmFragment;
import com.ldc.common.utils.ToastUtils;
import com.ldc.common.utils.ViewModelUtils;
import com.ldc.wandroid.adapter.WeChatNumberHistoryAdapter;
import com.ldc.wandroid.databinding.FragmentWeChatNumberHistoryBinding;
import com.ldc.wandroid.model.WeChatNumberHistoryModel;
import com.ldc.wandroid.ui.activitys.ShowArticleWebActivity;
import com.ldc.wandroid.viewmodel.WeChatNumberHistoryViewModel;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;

public class WeChatNumberHistoryFragment extends BaseMvvmFragment<FragmentWeChatNumberHistoryBinding, WeChatNumberHistoryViewModel> {


    private int currPage = 0;
    private String weChatId = "";
    private final WeChatNumberHistoryAdapter weChatNumberHistoryAdapter = new WeChatNumberHistoryAdapter();


    public void getWeChatNumber(BaseModel<WeChatNumberHistoryModel> result) {
        viewBinding.stateView.showContentView();
        viewBinding.rv.refreshView.finishRefresh();
        viewBinding.rv.refreshView.finishLoadMore();
        if (currPage == 0) {
            weChatNumberHistoryAdapter.setNewData(result.getData().getDatas());
        } else {
            weChatNumberHistoryAdapter.addNewData(result.getData().getDatas());
        }

    }

    public void unSelectCollectOriginIdResp(BaseModel<Object> data) {
        if (null == data) return;
        if (data.isFailed()) {
            ToastUtils.showShort(data.getErrorMsg());
        }
    }


    //初始化适配器
    private void initAdapter() {
        viewBinding.rv.refreshView.setOnRefreshLoadMoreListener(onRefreshLoadMoreListener);
        viewBinding.rv.recyclerView.setHasFixedSize(true);
        viewBinding.rv.recyclerView.setItemViewCacheSize(10);
        viewBinding.rv.recyclerView.setAdapter(weChatNumberHistoryAdapter);
        viewBinding.rv.recyclerView.setLayoutManager(new LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false));
        weChatNumberHistoryAdapter.setCommonViewClick(data -> ShowArticleWebActivity.actionStart(getActivity(), data.getTitle(), data.getLink()));
    }


    //
    private final OnRefreshLoadMoreListener onRefreshLoadMoreListener = new OnRefreshLoadMoreListener() {
        @Override
        public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
            currPage++;
            viewModel.getWechatNumberHiostoryReq(weChatId, currPage);

        }

        @Override
        public void onRefresh(@NonNull RefreshLayout refreshLayout) {
            currPage = 0;
            viewModel.getWechatNumberHiostoryReq(weChatId, currPage);
        }
    };

    @Override
    protected FragmentWeChatNumberHistoryBinding initViewBinding(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return FragmentWeChatNumberHistoryBinding.inflate(getLayoutInflater(), container, false);
    }

    @Override
    protected void main(Bundle savedInstanceState) {
        viewBinding.stateView.showLoadingView();
        initAdapter();
        currPage = 0;
        weChatId = requireArguments().getString("cid");
        viewModel.getCollect().observeForever(this::unSelectCollectOriginIdResp);
        viewModel.getWeChatNumber().observeForever(this::getWeChatNumber);
        viewModel.getWechatNumberHiostoryReq(weChatId, currPage);
    }

    @Override
    protected WeChatNumberHistoryViewModel initViewModel() {
        return ViewModelUtils.getViewModel(this, WeChatNumberHistoryViewModel.class);
    }
}
